package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.model.KeyWord;
import com.weipai.demo.model.KeyWordAttribute;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.KeyWordService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 赵项阳
 * @Date: 2019/7/2 16:50
 * @Version 1.0
 */

//关键字

@RestController
public class KeyWordController {

    @Autowired
    private KeyWordService keywordService;
    @Autowired
    private FileUploadService fileUploadService;

    //填写关键字
    @RequestMapping(value = "/keyword/save")
    public Response save(KeyWord keyWord){
     return keywordService.save(keyWord);
    }


    //前台展示关键字
    @RequestMapping(value = "/keyword/frontShow")
    public Response frontShow(Integer type){
        return keywordService.frontShow(type);
    }


    //后台分页展示关键字
    @RequestMapping(value = "/keyword/show")
    public LayuiTablePagingResult show(Integer page,Integer size,Integer type){
     return keywordService.show(page,size,type);
    }

    //后台分页展示关键字
    @RequestMapping(value = "/attribute/show")
    public LayuiTablePagingResult attributeShow(Integer page,Integer size,Integer id){
     return keywordService.attributeShow(page,size,id);
    }

    //修改关键字
    @RequestMapping(value = "/keyword/updateStatus")
    public Response updateStatus(Integer id,Integer status,String attribute,Integer type,String keyword,String title){
        return keywordService.updateStatus(id,status,attribute,type,keyword,title);
    }

    //删除关键字
    @RequestMapping(value = "/keyword/delete")
    public Response delete(Integer id){
        return keywordService.delete(id);
    }

    //添加属性
    @RequestMapping(value = "/attribute/save")
    public Response attributeSave(KeyWordAttribute keyWordAttribute, MultipartFile logo1, MultipartFile logoImg1, HttpServletRequest request){
        Response response = new Response();
        if (logo1 != null) {
            String result = fileUploadService.saveImgUpload(request, logo1);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
                return response;
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
                return response;
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
                return response;
            } else {
                keyWordAttribute.setLogo(result);
            }
        }else {
            keyWordAttribute.setLogo(request.getParameter("img"));
        }
        if (logoImg1 != null) {
            String result = fileUploadService.saveImgUpload(request, logoImg1);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
                return response;
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
                return response;
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
                return response;
            } else {
                keyWordAttribute.setLogoImg(result);
            }
        }else {
            keyWordAttribute.setLogoImg(request.getParameter("img1"));
        }
        return keywordService.saveAttribute(keyWordAttribute);
    }

    //前台关键字查询
     @RequestMapping(value = "/keyword/searchKeyword")
    public Response searchKeyword(){
        return keywordService.searchKeyword();
     }

    //删除属性
     @RequestMapping(value = "/attribute/delAttribute")
    public Response delAttribute(Integer id){
        return keywordService.delAttribute(id);
     }
}
